from selenium import webdriver
import time


def open_edge():
    # 配置
    browser = webdriver.Edge()
    # 启动浏览器
    return browser


def login(driver):
    url = 'https://acscrucible.honeywell.com/browse/ECC_BuildingsNA_SpyderTool/Trunk?_redir=login&focusedRev=5306'
    driver.get(url)
    # 找到表单
    # time.sleep(1)
    # search_value = driver.find_element_by_id('os_username')
    # search_value.send_keys('e391738')
    # search_value = driver.find_element_by_id('os_password')
    # search_value.send_keys('HON172well')
    # search_value = driver.find_element_by_id('os_login')
    # search_value.click()
    time.sleep(8)
    search_value = driver.find_element_by_id('selectedDirTreeNode')
    search_value.click()
    time.sleep(3)
    parent = driver.find_element_by_id("treeli81087892")
    i = 0
    list = []
    for i in range(0, 1):
        time.sleep(1)
        try:
            nodes = parent.find_elements_by_class_name('pathLink')
            if nodes.__len__() > 0:
                for node in nodes:
                    time.sleep(1)
                    node.click()
                    i = i + 1
                    print(i)
        except:
            continue

    time.sleep(1)

    for i in range(0, 2):
        try:
            # time.sleep(1)
            nodes = parent.find_elements_by_class_name('fileLink')
            if nodes.__len__() > 0:
                for node in nodes:
                    # time.sleep(1)
                    temp = node.get_attribute('href')
                    print(temp)
                    list.append(temp)
        except:
            continue

    setColl = set(list)

    fobj2 = open('C:\\Users\\h295061\\recv.csv', 'a')

    for i in setColl:
        driver.get(i)
        node = driver.find_element_by_id('latest-revision')
        temp = node.get_attribute('href')
        fobj2.write(str(temp) + '\n')
        # try:
        #    secondNodes = node.find_elements_by_class_name("tree-li")
        #    for secondNode in secondNodes:
        #        secondNode.click()
        #        time.sleep(5)
        #        print(secondNode)
        # except:
        #    secondNodes = node.find_elements_by_class_name("file-li")
        #    for secondNode in secondNodes:
        #        secondNode.click()
        #        file = driver.find_elements_by_id('latest-revision')
        #        print(file)


# driver.find_element_by_id('su').click()
# print(driver)


if __name__ == '__main__':
    driver = open_edge()
    login(driver)
